#!/usr/bin/env python
#
# crowd.py
#
# Contains the crowd class to keep track
# of conversations


import sys
sys.path.append('depends')

# Crowd is a singleton that will keep references
#  to active conversations.  Before calling the
#  class it must be instanciated by the
#  gatherCrowd function

# - gatherCrowd
# Instanciate the singleton Crowd class to
# prepare it to be called elseware
def gatherCrowd():
	"""
	Creates the one instance of Crowd and calls Crowd.gather() to instanciate the class
	"""
	newCrowd = Crowd()
	newCrowd.gather()


# Singleton class from Duncan Booth
# http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html
# subclasses of this will allow for only one instance of the class
# to exist and give the same instance to each call.
class Singleton(object):
	"""
	Provides a singleton implementation for python.  If a class extends Singleton only
	one instance of the class will exist and it will prevent multiple instances.
	"""
	_instance = None
	def __new__(single, *args, **kwargs):
		if not single._instance:
			single._instance = super(Singleton, single).__new__(single, *args, **kwargs)
		return single._instance

class Crowd(Singleton):
	"""
	Singleton class to hold conversation objects.  To instanciate call the gather
	function after receiving a new instance.  New conversations are automatically added so
	gather must be called before new conversations are made.
	"""
	def gather(self):
		"""
		Instanciates the class with an empty list.  Only call gather once before the class is used.
		Future calls will erase any conversation that had been added to the list
		"""
		self.conversations = []

	def addConversation(self, convo):
		"""
		Add conversation 'convo' to the list of conversations
		"""
		self.conversations.append(convo)

	def update(self):
		"""
		Call the update function on each conversation
		"""
		for con in self.conversations:
			con.update()

	def draw(self):
		"""
		Call the draw function for each conversation
		"""
		for con in self.conversations:
			con.draw()

	def checkClicked(self, pos):
		for con in self.conversations:
			conClicked = con.checkClicked(pos) 
			if conClicked != None:
				return conClicked

		# Nothing was clicked return none
		return None
		
		

	def __repr__(self):
		"""
		Represent Crowd as a string
		"""
		rprString = ""
		for con in self.conversations:
			rprString += ":" + str(con) + ":"
		return rprString
		


if __name__ == "__main__":
	gatherCrowd()
	newC = Crowd()
	print newC.conversations
